<template>
  <div style="padding:20px;">
    <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" fit highlight-current-row >
      <el-table-column label="开锁时间">
        <template slot-scope="scope">
          {{ scope.row.eventTs | parseTime('{y}-{m}-{d} {h}:{i}') }}
        </template>
      </el-table-column>
      <el-table-column label="开锁类型">
        <template slot-scope="scope">
          <el-tag :type="scope.row.eventType | typeColor" size="mini">{{ scope.row.eventType | typeFilter }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="钥匙编号">
        <template slot-scope="scope">
          {{ scope.row.keyId }}
        </template>
      </el-table-column>
    </el-table>
    <div v-if="list.length > 0" class="pagination-container">
      <el-pagination :current-page="listQuery.currentPage" :page-sizes="[10,25,50,100]" :page-size="listQuery.pageSize" :total="total" background layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange" @current-change="handleCurrentChange"/>
    </div>

    <back-to-top :custom-style="myBackToTopStyle" :visibility-height="10" :back-position="0" transition-name="fade"/>
  </div>
</template>

<script>
import { getEvent } from '@/api/device'
import BackToTop from '@/components/BackToTop'
export default {
  name: 'UnlockRecordPane',
  components: { BackToTop },
  filters: {
    typeColor(type) {
      const typeMap = {
        1: 'success',
        2: 'primary'
      }
      return typeMap[type]
    },
    typeFilter(type) {
      const typeMap = {
        1: '离线开锁',
        2: '在线开锁'
      }
      return typeMap[type]
    }
  },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: false,
      duration: [],
      listQuery: {
        currentPage: 1,
        pageSize: 25,
        serialNumber: this.$route.params['sn'],
        eventType: '1,2'
      },
      myBackToTopStyle: {
        right: '50px',
        bottom: '50px',
        width: '40px',
        height: '40px',
        'border-radius': '4px',
        'line-height': '45px',
        background: '#e7eaf1'
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    /* 获取开门记录列表 */
    fetchData() {
      this.listLoading = true
      if (this.duration && this.duration.length === 2) {
        this.listQuery.startTime = this.duration[0]
        this.listQuery.endTime = this.duration[1]
      } else {
        this.listQuery.startTime = ''
        this.listQuery.endTime = ''
      }
      getEvent(this.listQuery).then(response => {
        if (response.data) {
          this.list = response.data
          this.total = response.total
        }
      }).finally(() => {
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.currentPage = 1
      this.fetchData()
    },
    handleSizeChange(val) {
      this.listQuery.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.listQuery.currentPage = val
      this.fetchData()
    }
  }
}
</script>
